<template>
	<view>
		<dashboard :uppy="uppy"/>
	</view>
</template>

<script>
	
	import { Dashboard } from '@uppy/vue'
	
	import "@uppy/core/dist/style.css"
	import "@uppy/dashboard/dist/style.css"
	import "@uppy/core/dist/style.css"
	import "@uppy/status-bar/dist/style.css"
	
	import Uppy from '@uppy/core'
	import Webcam from '@uppy/webcam'
	import Tus from '@uppy/tus'
	var _self 
	export default {
		props:{
			limit: {
			  type: Number,
			  default: 5
			},
			url: {
			  type: String,
			  required: true
			},
			name: {
			  type: String,
			  default: 'file'
			},
			fileDescription: {
			  type: String,
			  default: ''
			}
		},
		data() {
			return {
				fileInfo:{
					fileName: "",
					fileSize: "",
					extension: "",
					fileGroup: "xiaohong",
					fileDescription: "",
					fileUrl: "",
					md5: ""
				}
			}
		},
		created() {
			_self = this
			this.uppy.on('complete', result => {
			  console.log('successful files:', result.successful)
			  if(result.successful.length > 0){
				  // 代表上传成功 遍历保存数据 
				  for (var i = 0; i < result.successful.length; i++) {
				  	 _self.fileInfo.fileName = result.successful[0].name
				  	 _self.fileInfo.fileSize = result.successful[0].size
				  	 _self.fileInfo.extension = result.successful[0].extension
				  	 _self.fileInfo.fileDescription = _self.fileDescription
				  	 _self.fileInfo.fileUrl = "https://file.xiaohong.work/xiaohong"+result.successful[0].uploadURL.split("/xiaohong").pop()
				  	 _self.fileInfo.md5 = result.successful[0].uploadURL.split('/').pop()
					 
					 // 保存数据
					 uni.request({
					 	url: _self.utils.basicUrl+"/file/add",
					 	method: "POST",
					 	data: {
							..._self.fileInfo
						},
					 	success: function(res) {
					 		if(res.data.code === 20000){
								uni.showToast({
									icon:"none",
									title:"上传成功"
								})
					 		}else{
					 			_self.utils.requestFail(res)
					 		}
					 	},
					 	fail: () => {
					 		uni.showToast({
					 			icon:"none",
					 			title: '网络错误'
					 		})
					 	},
					 	complete() {
					 		
					 	}
					 })
					 
				  }
			  }
			  // console.log(result.successful.length)
			  // console.log(result.successful[0].name)
			  // console.log(result.successful[0].size)
			  // console.log(result.successful[0].type)
			  // console.log(result.successful[0].extension)
			  // // "http://119.29.67.157:8080/xiaohong/big/upload/60740c2fc83840e1b7d8d35a23e5927a"
			  // console.log(result.successful[0].uploadURL.split('/').pop())
			  // console.log('failed files:', result.failed)
			})
		},
		components: {
		   Dashboard,
		},
		computed: {
		   uppy: () => new Uppy()
		   .use(Tus, { 
			   endpoint: 'http://119.29.67.157:8080/xiaohong/big/upload/' ,
		   })
		   
		},
		beforeDestroy () {
		   this.uppy.close()
		},
		methods: {
			
		}
	}
</script>

<style>

</style>
